package com.rykj.qxj;


import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;


import javax.servlet.MultipartConfigElement;


@Configuration
@ServletComponentScan
@SpringBootApplication
public class Application {


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

        //服务重启时,重启正常的任务
     /*   TaskService  taskService =  (TaskService) SpringUtil.getBean("taskService");
        List<Map<String,Object>>  tasks = taskService.getAllNormalTasks();
        if(!CollectionUtils.isEmpty(tasks)){
            for (Map<String,Object> taskMap:tasks) {
                String taskStatus = (String) taskMap.get("taskStatus");
                String taskName = (String) taskMap.get("taskName");
                String executeRate = (String) taskMap.get("executeRate");
                Integer taskId = (Integer) taskMap.get("taskId");
                QuartzManager
                .addJob(taskName,taskId,executeRate);
                //如果是禁用的任务改禁用
                if("disable".equals(taskStatus)){
                    QuartzManager.shutdownJob(taskName);
                }
            }
        }*/
    }

    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("50480KB");
        factory.setMaxRequestSize("50480KB");
        return factory.createMultipartConfig();
    }
    /**
     * 覆盖方法configureMessageConverters，使用fastJson
     *
     * @return
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        //1、定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2、添加fastjson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //3、在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4、将convert添加到converters中
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

}
